﻿using System;
using System.Collections.Generic;
using _5E.Infrastructure.Querying;
using _5E.Infrastructure.UnitOfWork;
using _5E.NHibernateRepository;
using _5E.NHibernateRepository.Repository;
using NHibernate.Criterion;
using WholesaleOnline.Model;
using WholesaleOnline.Model.Repository;

namespace WholesaleOnline.Repository
{
    public class SellerAttributeRepository : RepositoryBase<WoSellerAttribute, Guid>, ISellerAttributeRepository
    {
        public SellerAttributeRepository(IUnitOfWork uow) : base(uow) { }

        public IList<WoAttribute> FindFor(Guid sellerId, string attributeName)
        {
            IList<WoAttribute> attributes = SessionFactory.CurrentSession.QueryOver<WoSellerAttribute>()
                .Where(sc => sc.SellerId == sellerId)
                .Inner.JoinQueryOver<WoAttribute>(seller => seller.Attribute)
                .Where(Restrictions.And(
                    Restrictions.IsNull(PropertyNameHelper.ResolvePropertyName<WoAttribute>(attr => attr.ParentId)),
                    Restrictions.Eq(PropertyNameHelper.ResolvePropertyName<WoAttribute>(attr => attr.Name), attributeName)))
                .OrderBy(attribute => attribute.SortOrder).Asc
                .Select(seller => seller.Attribute)
                .List<WoAttribute>();
            return attributes;
        }
    }
}
